package com.wangsb;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Description: TODO
 * @author: shenbing.wang@hand-china
 * @date: 2021年08月07日 9:58
 */
//1、排序和序列化需要实现WritableComparable接口
public class SortBean implements WritableComparable<SortBean> {

    private Long sortFiled;
    private Long sort;

    // 2. 反序列化时，需要反射调用空参构造函数，所以必须有空参构造
    public SortBean() {
    }

    public SortBean(Long sortFiled, Long sort) {
        this.sortFiled = sortFiled;
        this.sort = sort;
    }

    public Long getSortFiled() {
        return sortFiled;
    }

    public void setSortFiled(Long sortFiled) {
        this.sortFiled = sortFiled;
    }

    public Long getSort() {
        return sort;
    }

    public void setSort(Long sort) {
        this.sort = sort;
    }

    //实现compareTo方法
    @Override
    public int compareTo(SortBean o) {
        int result;
        // 按照总流量大小，倒序排列
        if (this.sortFiled > o.getSortFiled()) {
            result = 1;
        } else if (this.sortFiled < o.getSortFiled()) {
            result = -1;
        } else {
            result = 0;
        }
        return result;

    }

    /**
     * desc 实现序列化接口
     *
     * @param dataOutput
     * @return void
     * @author shenbing.wang
     * @date 2021/8/7 10:49
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(sortFiled);
        dataOutput.writeLong(sort);
    }

    /**
     * desc 反序列化与序列化顺序需要一致
     *
     * @param dataInput
     * @return void
     * @author shenbing.wang
     * @date 2021/8/7 10:49
     */
    @Override
    public void readFields(DataInput dataInput) throws IOException {

        this.sortFiled = dataInput.readLong();
        this.sort = dataInput.readLong();

    }

    //实现toString方法  按照输出的要求
    @Override
    public String toString() {
        return sort + "\t" + sortFiled;
    }
}
